"""
	Developed by 
	Andrea Stagi <stagi.andrea@gmail.com>

	Tiny4py: python wrapper to use main url shortener services in your apps
	Copyright (C) 2010 Andrea Stagi

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU Lesser General Public License as published 
	by the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU Lesser General Public License for more details.

	You should have received a copy of the GNU Lesser General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
"""

import tiny4py
from sys import argv

def MyCallback(shorturl):
	print "Hi, I'm a callback function. Shorturl is %s" %shorturl

def main():

	short_tiny=""
	short_bitly=""
	short_google=""

	#Creating our shorteners for test

	google_shortener=tiny4py.getShortener(tiny4py.GOOGLE_ID)
	bitly_shortener=tiny4py.getShortener(tiny4py.BITLY_ID)
	tinyurl_shortener=tiny4py.getShortener(tiny4py.TINYURL_ID)

	#Trying to use goo.gl

	try:
		short_google = google_shortener.getShorturl(argv[1])
		print short_google

	except tiny4py.ShortenerError,sh:
		print sh

	#Trying to use goo.gl and callbacks

	try:
		google_shortener.getShorturl(argv[1],callback = MyCallback)

	except tiny4py.ShortenerError,sh:
		print sh

	#Trying to change user with bitly and short an url

	try:
		bitly_shortener.setApi("tiny4py","R_b69f9ca899f8e7ef59fd5c27272ec26c")
		bitly_shortener.getShorturl(argv[1])

	except tiny4py.ShortenerError,sh:
		print sh

	#Trying to use bit.ly

	try:
		short_bitly = bitly_shortener.getShorturl(argv[1])
		print short_bitly

	except tiny4py.ShortenerError,sh:
		print sh

	#Trying to use tinyurl

	try:
		short_tiny=tinyurl_shortener.getShorturl(argv[1])
		print short_tiny

	except tiny4py.ShortenerError,sh:
		print sh

	#Trying create a QRCode with bit.ly

	try:
		print "Now let's try to create an image with the relative QRCode using Bit.ly"

		myqrcodefile=bitly_shortener.getQRCode(short_bitly)

		localFile = open("myqrcode_bitly.png", 'wb')
		localFile.write(myqrcodefile)
		localFile.close()

	except tiny4py.ShortenerError,sh:
		print sh

	#Trying create a QRCode with goo.gl and save it in a file ('file' parameter)

	try:
		print "Now let's try to create an image with the relative QRCode using Goo.gl"

		google_shortener.getQRCode(short_google,{'file':"myqrcode_google.png"})

	except tiny4py.ShortenerError,sh:
		print sh

	#Trying bit.ly statistics

	try:
		bitly_usr_clicks=bitly_shortener.getUserClicks(short_bitly)
		print "Short URL %s has %d user clicks" % (short_bitly,bitly_usr_clicks)

	except tiny4py.ShortenerError,sh:
		print sh

	try:
		bitly_glb_clicks=bitly_shortener.getGlobalClicks(short_bitly)
		print "Short URL %s has %d global clicks" % (short_bitly,bitly_glb_clicks)

	except tiny4py.ShortenerError,sh:
		print sh


	#Trying some reverse operation (the same for every shortener)

	try:
		print google_shortener.getLongurl(short_google)

	except tiny4py.ShortenerError,sh:
		print sh

	try:
		print bitly_shortener.getLongurl(short_tiny)

	except tiny4py.ShortenerError,sh:
		print sh

if __name__ == '__main__':
	main()



